//
//  ListNodes.swift
//  soapp_Example
//
//  Created by zhu xietong on 2018/2/13.
//  Copyright © 2018年 CocoaPods. All rights reserved.
//

import Foundation
import AsyncDisplayKit
import soapp
open class ASRuleKit: MoDisplayNode {
    open class var space:CGFloat{
        get{
            return 0
        }
    }
    open class var height:CGFloat{
        get{
            return 100.0
        }
    }
    
}

class ASRuleNode<Kit:ASRuleKit>: MoDisplayNode{
  
    
    let listNode = ASDisplayNode()
    override func addLayoutRules() {
        
        let bkNode = ASDisplayNode()
        let bk = [
            _padding(0),
            bkNode
            ] as [Any]
        
        bkNode.backgroundColor = UIColor.table_bg_color
        
        
        flexRules = [
            |+.row,_shrink(1),
            [
                [
                    |+.column,|2,_shrink(1),_sid("list"),_wrap,_padding(4),
                    ],
                ]
        ]
    }
    
    var nodes:[Kit] = [Kit]()
    
    override func mo_model_reload() {
        
        for one in nodes
        {
            one.removeFromSupernode()
        }
        
        var subnodes = [Kit]()
        weak var wself = self
        DataNode<NSMutableArray>.path(node: "sub", mo.model)?.list({ (dict:NSMutableDictionary, i) in
            let subkit = Kit()
            dict["__index"] = "\(i)"
            wself?.addSubnode(subkit)
            subkit.mo.model = dict
            subnodes.append(subkit)
        })
        self.nodes = subnodes
        
    }
    
    override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
        let spec = super.layoutSpecThatFits(constrainedSize)
        if let list = spec.subSpec(specId: "list") as? ASStackLayoutSpec
        {
            list.children =  nodes
        }
        return spec
    }
    
}

